<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"
	import="java.text.*,java.util.*,java.io.*,java.sql.*,com.telecom.cms.dto.*,com.telecom.cms.dao.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Insert title here</title>
	</head>
	<body>
		<%
			response.reset();
			String data = "";
			SimpleDateFormat dataFormat = new SimpleDateFormat("yyyyMMddHHmm");
			java.util.Date today = new java.util.Date();
			String dateToday = dataFormat.format(today);
			response.setHeader("Content-Disposition", "attachment; filename=\""
					+ dateToday + ".csv" + "\"");
			response.setCharacterEncoding("GB2312");

			response.setContentType("text/plain;charset=GB2312");
			//	PrintWriter ot = response.getWriter();
			OutputStream os = response.getOutputStream();

			String sql = (String) request.getSession().getAttribute("SQL");

			List list = (List) request.getSession().getAttribute("Sqllist");

			data += "客户ID,";

			data += "客户名称,";

			for (int i = 0; i < list.size(); i++) {
				SEG_BIZ_ATOM_DEFDTO dto = (SEG_BIZ_ATOM_DEFDTO) list.get(i);
				data += (dto.getBIZ_ATOM_NAME().trim() + ",");
			}
			data += "\n";

			os.write(data.toString().getBytes("GBK"));

			Connection con = GetConnect.getCsConnection();
			PreparedStatement psmt = con.prepareStatement(sql);

			ResultSet rset = psmt.executeQuery();
			//这里加入提示，如果超过65536条则提示用户无法用excel打开

			while (rset.next()) {

				for (int j = 0; j < list.size() + 2; j++) {

					//data += (rset.getString(j + 1).trim() + ",");
					if (rset.getString(j + 1) == null) {

				os.write("0".toString().getBytes("GBK"));
					} else {
				os.write(rset.getString(j + 1).trim().toString()
				.getBytes("GBK"));
					}
					os.write(',');

				}

				//data += "\n";
				os.write('\n');

			}

			//StringBuffer str = new StringBuffer();
			//str.append(data);

			//os.write(str.toString().getBytes("GBK"));

			os.flush();
			os.close();

			out.clear();
			out = pageContext.pushBody();
		%>
	</body>
</html>
